package com.zx.base.widget;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zx.base.ctrl.BaseActivity;
import com.zx.tradeclient.R;
import com.zx.tradeclient.app.TradeApp;

public class ExpanderDialog {

	private Context context;
	private LDialog dlg;
	private DialogListener listen;
	// private int type;
	private TradeApp app;
	private String[] generalsTypes;
	private String[][] generals;

	public static interface DialogListener {
		public void endme(String group, String value, int groupid, int valueid);
	}

	public ExpanderDialog(Context context, DialogListener dialogListener, int type) {
		this.listen = dialogListener;
		this.context = context;
		// this.type = type;
		app = (TradeApp) context.getApplicationContext();
		if (type == 1) {
			this.generalsTypes = app.provinces;
			this.generals = app.citys;
		} else if (type == 2) {
			this.generalsTypes = app.productgroup;
			this.generals = app.producttypes;
		} else if (type == 3) {
			this.generalsTypes = app.tradeways;
			this.generals = new String[generalsTypes.length][];
			for (int i = 0; i < generalsTypes.length; i++) {
				this.generals[i] = new String[1];
				this.generals[i][0] = app.tradeways[i];
			}

		} else if (type == 4) {
			this.generalsTypes = new String[] { "订单状态" };
			this.generals = new String[this.generalsTypes.length][app.ordertypes.length];
			for (int i = 0; i < app.ordertypes.length; i++) {
				this.generals[0][i] = app.ordertypes[i];
			}

		}

	}

	public void show() {
		dlg = new LDialog(context);// 创建Dialog并设置样式主题
		dlg.setCanceledOnTouchOutside(false);
		dlg.show();
		WindowManager m = ((BaseActivity) context).getWindowManager();
		Display d = m.getDefaultDisplay(); // 为获取屏幕宽、高
		WindowManager.LayoutParams p = dlg.getWindow().getAttributes();
		// 获取对话框当前的参数值
		p.x = 0; // 设置位置 默认为居中
		p.y = 0; // 设置位置 默认为居中
		p.height = (int) (d.getHeight()); // 高度设置为屏幕的0.6
		p.width = (int) (d.getWidth()); // 宽度设置为屏幕的0.95
		dlg.getWindow().setAttributes((android.view.WindowManager.LayoutParams) p);
	}

	public class LDialog extends AlertDialog {
		public LDialog(Context context, int theme) {
			super(context, theme);
		}

		public LDialog(Context context) {
			super(context);
		}

		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.expandablelistview);

			final ExpandableListAdapter adapter = new BaseExpandableListAdapter() {

				// 自己定义一个获得文字信息的方法
				TextView getTextView() {
					AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 64);
					TextView textView = new TextView(context);
					textView.setLayoutParams(lp);
					textView.setGravity(Gravity.CENTER_VERTICAL);
					textView.setPadding(80, 0, 0, 0);
					return textView;
				}

				// 重写ExpandableListAdapter中的各个方法
				@Override
				public int getGroupCount() {
					return generalsTypes.length;
				}

				@Override
				public Object getGroup(int groupPosition) {
					return generalsTypes[groupPosition];
				}

				@Override
				public long getGroupId(int groupPosition) {
					return groupPosition;
				}

				@Override
				public int getChildrenCount(int groupPosition) {
					return generals[groupPosition].length;
				}

				@Override
				public Object getChild(int groupPosition, int childPosition) {
					return generals[groupPosition][childPosition];
				}

				@Override
				public long getChildId(int groupPosition, int childPosition) {
					return childPosition;
				}

				@Override
				public boolean hasStableIds() {
					return true;
				}

				@Override
				public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
					LinearLayout ll = new LinearLayout(context);
					ll.setOrientation(0);

					TextView textView = getTextView();
					textView.setTextColor(Color.BLACK);
					textView.setTextSize(20);
					textView.setText(getGroup(groupPosition).toString());
					ll.addView(textView);

					return ll;
				}

				@Override
				public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
					LinearLayout ll = new LinearLayout(context);
					ll.setOrientation(0);
					TextView textView = getTextView();
					textView.setTextColor(Color.GRAY);
					textView.setTextSize(18);
					textView.setText(getChild(groupPosition, childPosition).toString());
					ll.addView(textView);
					return ll;
				}

				@Override
				public boolean isChildSelectable(int groupPosition, int childPosition) {
					return true;
				}

			};

			ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.list);
			expandableListView.setAdapter(adapter);

			// 设置item点击的监听器
			expandableListView.setOnChildClickListener(new OnChildClickListener() {

				@Override
				public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {

					listen.endme("" + adapter.getGroup(groupPosition), "" + adapter.getChild(groupPosition, childPosition), groupPosition, childPosition);
					dlg.dismiss();

					return false;
				}
			});

		}

	}

	public void setListen(DialogListener listen) {
		this.listen = listen;
	}

}
